package org.example.xml;

import lombok.Data;
import org.example.parse.PropertyParser;
import org.example.parse.XPathParser;
import org.w3c.dom.CharacterData;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

@Data
public class XNode {

    private final XPathParser xPathParser;
    private final Node node;
    private String name;
    //属性列表
    private final Properties attributes;
    private String body;

    public XNode(XPathParser xPathParser, Node node) {
        this.xPathParser = xPathParser;
        this.node = node;
        this.name = node.getNodeName();
        //解析属性
        this.attributes = parseAttributes(node);
        //解析内容
        this.body = getBodyData(node);
    }

    private String getBodyData(Node child) {
        if (child.getNodeType() == Node.CDATA_SECTION_NODE
                || child.getNodeType() == Node.TEXT_NODE) {
            String data = ((CharacterData) child).getData();
            data = PropertyParser.parse(data, null);
            return data;
        }

        NodeList children = child.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node itemNode = children.item(i);
            String data = getBodyData(itemNode);
            if (data != null) {
                return data;
            }
        }
        return null;
    }

    public XNode evalNode(String environments) {
        return xPathParser.evalNode(node,environments);
    }

    private Properties parseAttributes(Node n) {
        Properties attributes = new Properties();
        NamedNodeMap attributeNodes = n.getAttributes();
        if (attributeNodes != null) {
            for (int i = 0; i < attributeNodes.getLength(); i++) {
                Node attribute = attributeNodes.item(i);
                String value = PropertyParser.parse(attribute.getNodeValue(), null);
                attributes.put(attribute.getNodeName(), value);
            }
        }
        return attributes;
    }


    public List<XNode> getChildren() {
        List<XNode> children = new ArrayList<>();
        NodeList nodeList = node.getChildNodes();
        if (nodeList != null) {
            for (int i = 0, n = nodeList.getLength(); i < n; i++) {
                Node node = nodeList.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    children.add(new XNode(xPathParser, node));
                }
            }
        }
        return children;
    }

    public String getStringAttribute(String name) {
        String value = attributes.getProperty(name);
        return value == null ? "" : value;
    }

    public Properties getChildrenAsProperties() {
        Properties properties = new Properties();
        for (XNode child : getChildren()) {
            String name = child.getStringAttribute("name");
            String value = child.getStringAttribute("value");
            if (name != null && value != null) {
                properties.setProperty(name, value);
            }
        }
        return properties;
    }

    //解析节点
    public List<XNode> evalNodes(String expression) {
        return xPathParser.evalNodes(node,expression);
    }


    public String getName() {
        return node.getNodeName();
    }
}
